package com.foreknow.service.impl;

import com.foreknow.dao.GuestbookDao;
import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Guestbook;
import com.foreknow.model.student;
import com.foreknow.service.GuestbookService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookServiceImpl implements GuestbookService {
    private GuestbookDaoImpl dao = new GuestbookDaoImpl();
    @Override
    public boolean add(Guestbook gb) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        dao.setConnection(conn);
        boolean isRigth = false;
        try {
            isRigth = dao.save(gb);
            //dml操作需要提交事物
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRigth;
    }

    @Override
    public List<Object> showAll() {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        dao.setConnection(conn);
        GuestbookDao gst=new GuestbookDaoImpl();
        List<Object> queryAll = null;
        try {
            queryAll = gst.getQueryAll();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return queryAll;
    }

    @Override
    public boolean deleteById(int id)  {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        dao.setConnection(conn);
        GuestbookDao guestbookDao=new GuestbookDaoImpl();
        boolean b = false;
        try {
            b = guestbookDao.deleteById(id);
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        if (b) {
            
        }
        return b;
    }

    @Override
    public boolean update(Guestbook gb) throws SQLException {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        dao.setConnection(conn);
        GuestbookDao guestbookDao=new GuestbookDaoImpl();
        boolean IsUpdate = guestbookDao.update(gb);
        if (IsUpdate) {
            conn.commit();
        }else {
            conn.rollback();
        }
        return IsUpdate;
    }

    @Override
    public Guestbook getQueryById(int id) throws SQLException {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        dao.setConnection(conn);
        GuestbookDao guestbookDao=new GuestbookDaoImpl();
        Guestbook bg = guestbookDao.getQueryById(id);
        return bg;
    }


}
